package com.yr.controller;

import com.yr.entity.user;
import com.yr.service.userservice;
import com.yr.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

public class usercontroller {
    @Autowired
    private userservice userService;

    //查询订单列表
    @GetMapping("/list")
    public R list(){
        return R.success().data("users",userService.list());
    }
    //添加
    @PostMapping("/add")
    public R add(@RequestBody user user){
        if(userService.save(user)) return R.success().message("添加成功！");
        else return R.error().message("添加失败！");
    }

    //修改用户
    @PostMapping("/update")
    public R update(@RequestBody user user){
        if(userService.updateById(user)) return   R.success().message("修改成功！");
        else return R.error().message("修改失败！");
    }
   //删除user
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){
        String[] str = ids.split(",");
        List<String> IDs = Arrays.asList(str);
        if(userService.removeByIds(IDs)) return  R.success().message("删除成功!") ;
        else return R.error().message("删除失败!");
    }

}